package Models;

import java.util.ArrayList;

/**
 * @author Erik
 */
public class MachineCollection
{
	private ArrayList<Machine> machines;
    private static MachineCollection instance = new MachineCollection();

	private MachineCollection()
	{
		this.machines = new ArrayList<Machine>();
		Machine m = new Machine("fisk");
		this.machines.add(m);
		Machine n = new Machine("tog");
		n.setActivated(false);
		n.setAvailable(false);
		this.machines.add(n);
		//this.machines.add(new Machine("Gulvsliber"));
		//this.machines.add(new Machine("Jackhammer"));
		//this.machines.add(new Machine("Fiskestang"));
		//this.machines.add(new Machine("Flammekaster"));
	}

	public static void main(String arg[])
	{
		MachineCollection asd = MachineCollection.getInstance();
		asd.testFindMachine();
	}

	public void testFindMachine()
	{
		ArrayList<Machine> test1 = findMachine("fisk", true);
		if (test1.size() == 1)
		{
			System.out.println("test 1 gik godt");
		}
		else
		{
			System.out.println(test1.size() +" test 1 gik ikke godt");
		}
		ArrayList<Machine> test2 = findMachine(null, true);
		if (test2.size() == 1)
		{
			System.out.println("test 2 gik godt");
		}
		else
		{
			System.out.println("test 2 gik ikke godt");
		}
		ArrayList<Machine> test3 = findMachine(null, false);
		if (test3.size() == 0)
		{
			System.out.println("test 3 gik godt");
		}
		else
		{
			System.out.println(test3.size() +" test 3 gik ikke godt");
		}
	}

    public static MachineCollection getInstance()
	{
		return MachineCollection.instance;
	}

	public boolean addMachine(Machine newMachine)
	{
        return this.machines.add(newMachine);
	}

	public ArrayList<Machine> findMachine(String name, boolean available)
	{
        ArrayList<Machine> result = new ArrayList<Machine>();

        for (Machine machine : this.machines)
		{
			boolean found = false;
			if (machine.isActivated())
			{
				if (name != null)
				{
					if(machine.getName().toLowerCase().contains(name.toLowerCase()))
					{ found = true; }
				}
				else if (machine.isAvailable() == available)
				{ found = true; }

				if (found)
				{
					result.add(machine);
				}
			}
        }
		return result;
	}

	public Machine getMachine(int id)
	{
        for(Machine machine : this.machines){
            if(machine.getId() == id){
                return machine;
            }
        }
		return null;
	}

	public ArrayList<Machine> getAllMachines()
	{
		ArrayList<Machine> activatedMachines = new ArrayList<Machine>();
		for (Machine machine : this.machines)
		{
			if(machine.isActivated())
			{
				activatedMachines.add(machine);
			}
		}
		return activatedMachines;
	}

	public void removeMachine(Machine machine)
	{
        machine.setActivated(false);
	}
}
